package com.sanalkitaplik.usermanagement;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import com.sanalkitaplik.hibernateutil.StaticInfo;
import com.sanalkitaplik.hibernateutil.UsersUtil;
import com.sanalkitaplik.packageclassdto.UsersInfoDTO;
import com.sanalkitaplik.packageclassdto.UsersLoginInfoDTO;

public class UserRegistrationBean {

	private UserRegistration userRegistration;
	private List<SelectItem> userRoles;

	private boolean errorUserName;
	private boolean errorPassword;
	private boolean errorEmail;
	private boolean errorFirstname;
	private boolean errorLastname;
	
	private boolean success;
	
	public UserRegistrationBean() {
		resetErrors();
		success = false;
		userRegistration = new UserRegistration();
		userRegistration.setEmailHostAddress("etu.edu.tr");
		userRoles = new ArrayList<SelectItem>();
		ArrayList<String> userRoleNames = null;
		try {
			userRoleNames = StaticInfo.getTypeName();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (userRoleNames != null) {
			for (int i=0; i<userRoleNames.size(); i++)
				userRoles.add(new SelectItem(userRoleNames.get(i)));
		}
	}
	
	public boolean getSuccess() {
		return success;
	}
	
	public boolean getErrorUserName() {
		return errorUserName;
	}

	public boolean getErrorPassword() {
		return errorPassword;
	}

	public boolean getErrorEmail() {
		return errorEmail;
	}

	public boolean getErrorFirstname() {
		return errorFirstname;
	}

	public boolean getErrorLastname() {
		return errorLastname;
	}
	
	private void resetErrors() {
		errorEmail = false;
		errorFirstname = false;
		errorLastname = false;
		errorPassword = false;
		errorUserName = false;
	}
	
	public List<SelectItem> getUserRoles() {
		return userRoles;
	}
	
	public void setUserRegistration(UserRegistration userRegistration) {
		this.userRegistration = userRegistration;
	}
	
	public UserRegistration getUserRegistration() {
		return this.userRegistration;
	}
	
	public String register() {
		int l;
		String username = userRegistration.getUserName();
		String password = userRegistration.getPassword();
		String passwordCheck = userRegistration.getPasswordCheck();
		String emailAccountName = userRegistration.getEmailAccountName();
		String emailAccountNameCheck = userRegistration.getEmailAccountNameCheck();
		String firstName = userRegistration.getFirstName();
		String lastName = userRegistration.getLastName();
		resetErrors();
		if ((l = username.length()) < 6 || l > 20) {
			errorUserName = true;
		}
		if ((l = password.length()) < 6 || l > 20 ||
				!password.equals(passwordCheck)) {
			errorPassword = true;
		}
		if (emailAccountName.isEmpty() || !emailAccountName.equals(emailAccountNameCheck)) {
			errorEmail = true;
		}
		if (firstName.isEmpty()) {
			errorFirstname = true;
		}
		if (lastName.isEmpty()) {
			errorLastname = true;
		}
		if (errorUserName || errorPassword || errorEmail || errorFirstname || errorLastname) {
			return null;
		}
		UsersLoginInfoDTO loginInfo = new UsersLoginInfoDTO();
		UsersInfoDTO info = new UsersInfoDTO();
		loginInfo.setUsername(username);
		loginInfo.setUser_password(password);
		loginInfo.setUser_email(emailAccountName+"@"+userRegistration.getEmailHostAddress());
		info.setFirst_name(firstName);
		info.setLast_name(lastName);
		try {
			UsersUtil.userRegister(info, loginInfo, userRegistration.getUserRoleName());
			success = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "registerSuccess";
	}
	
}
